package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Goods;
import com.ruoyi.market.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 获取商品列表
     */
    @PostMapping("/goodsList")
    public TableDataInfo list(@RequestBody Goods goods)
    {
        startPageByPost();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('market:goods:export')")
    @PostMapping("/goodsExport")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品数据");
    }

    @Log(title = "商品管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('market:goods:import')")
    @PostMapping("/goodsImportData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        List<Goods> goodsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = goodsService.importGoods(goodsList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/goodsImportTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.importTemplateExcel(response, "商品数据");
    }

    /**
     * 根据商品编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:goods:query')")
    @GetMapping(value = { "/", "/{goodsId}" })
    public AjaxResult getInfo(@PathVariable(value = "goodsId", required = false) Long goodsId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(goodsId))
        {
            Goods sysGoods = goodsService.selectGoodsById(goodsId);
            ajax.put(AjaxResult.DATA_TAG, sysGoods);
        }
        return ajax;
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('market:goods:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Goods goods)
    {
        if (!goodsService.checkGoodsUnique(goods))
        {
            return error("新增商品'" + goods.getGoodsTitle() + "'失败，已存在");
        }
        goods.setCreateBy(getUsername());
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('market:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Goods goods)
    {
        if (!goodsService.checkGoodsUnique(goods))
        {
            return error("修改商品'" + goods.getGoodsTitle() + "'失败，已存在");
        }
        goods.setUpdateBy(getUsername());
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('market:goods:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsService.deleteGoodsByIds(goodsIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/goodsChangeStatus")
    public AjaxResult changeStatus(@RequestBody Goods goods)
    {
        goods.setUpdateBy(getUsername());
        return toAjax(goodsService.updateGoodsStatus(goods));
    }
}
